<script src="${ctx}/assets/js/jquery.validate.js"></script>
<script>	
var flag="";//事件标识
var orgId = "${sessionOrg}";
 $(function(){
	    
		 
	    Orginput.init($("#authedOrg"),{defaultOrgId:"${authInfo.authedOrg}",defaultOrgName:"<@libMacro.orgName orgId="${authInfo.authedOrg}" />",
	    		afterSelect: function() {
	    		}
	    });  
	    
	    //人员选择插件
	 	 $("#authedUser").userinput({
		 	belongAreaId:'authedOrg',
		 	defaultUserName:"<@libMacro.userName userId="${authInfo.authedUser}"/>",
		 	defaultUserCode:"${authInfo.authedUser}",
		 	beforeSelect: function(){
		    	if ($("#authedOrg").val() == "") {
		    		alert("请选择被授权人所属机构！");
		    		return false;
		    	}
	    	}
		 }); 
	    
	 	 Orginput.init($("#authOrg"),{defaultOrgId:"${authInfo.authOrg}",defaultOrgName:"<@libMacro.orgName orgId="${authInfo.authOrg}" />",
	    		afterSelect: function() {
	    		}
	     }); 
		 
	    
		if($("#isAdministrator").val()=='1'){		
			
			  //人员选择插件
			  $("#authUser").userinput({
			 	belongAreaId:'authOrg',
			 	defaultUserName:"<@libMacro.userName userId="${authInfo.authUser}"/>",
			 	defaultUserCode:"${authInfo.authUser}",
			 	beforeSelect: function(){
			    	if ($("#authOrg").val() == "") {
			    		alert("请选择授权人所属机构！");
			    		return false;
			    	}
		    	},
		    	onChange: function(){
		    		setRoleStr();
		    	}
			 }); 
		}else{
			//$("#authOrg").hide();
			//$("#authUser").hide();
		}
       
		initDetail();
	});
 
    function setRoleStr(){
    	var roleParams = "authUser="+$("#authUser").val();
    	 $.ajax({
	        	async : false,
		    	url: "${ctx}/auth/getRoleStrByUserId.do",
		    	type:"post",
		    	data: roleParams,
		    	dataType:"json", 
		    	success: function(data){				    					    		
				   	if(data[0]=="ok"){
				   		setRoleCheckbox(data);
				   	}else{
				   		alert(data[1]);
				   	}
		    	}
	       	}); 
    }
    
    function setRoleCheckbox(data){
    	$("#roleStrId").html("");
    	for(var i=0;i<data[1];i++){
    		$("#roleStrId").append('<input type="checkbox" id="userRoles-'+i+'" value="'+data[i+2].roleId+'" name="authedRole">');
    		$("#roleStrId").append('<label class="checkboxLabel" for="userRoles-'+i+'">'+data[i+2].roleName+'</label>');
    	}
    }
 
    function initDetail(){
    	var sendFlag = $("#sendFlag").val();
    	if(sendFlag==1){
    		//新增
    		$("#btnAModify").hide();
    		$("#btnADelete").hide();
    		$("#btnASave").show();
    		$("#btnACancle").hide();
    		isAdministrator();
    	
    		$("#authOrgName").hide();
    		$("#authUserName").hide();
    	}else if(sendFlag==2){
    		//查询
    		$("#btnAModify").show();
        	$("#btnADelete").show();
        	$("#btnASave").hide();
        	$("#btnACancle").hide();
        	$("#authUser").hide();
        	$("#authedUser").hide();
    		disabledAll();
    	}
    }
    
    function isAdministrator(){
    	if($("#isAdministrator").val()!='1'){
			$("#authUser").attr("disabled",true);
			$("#authOrg").attr("disabled",true);
			$("#authStatus").attr("disabled",false);
		}else{
			$("#authStatus").attr("disabled",true);
		}
    }
    
    function disabledAll(){
    	$("#authDetailForm input[type='text']").attr("disabled",true);
    	$("#authDetailForm input[type='textarea']").attr("disabled",true);
    	$("#authDetailForm select").attr("disabled",true);
    	$("#authDetailForm a").attr("disabled",true);
    	$("#authDetailForm textarea").attr("disabled",true);
    	$("#authRolesTable input").attr("disabled", true);
    	$("#tab1").find("input[type='checkbox']").each(function() { 
			$(this).attr("disabled", true); 
		});  
    }
    
    function enabledAll(){
    	$("#authDetailForm input[type='text']").attr("disabled",false);
    	$("#authDetailForm input[type='textarea']").attr("disabled",false);
    	$("#authDetailForm select").attr("disabled",false);
    	$("#authDetailForm textarea").attr("disabled",false);
    	$("#authDetailForm a").attr("disabled",false);
    	$("#authRolesTable input").attr("disabled", false);
    	$("#tab1").find("input[type='checkbox']").each(function() { 
			$(this).attr("disabled", false); 
		}); 
    }

    //保存信息
	function save(){ 
		enabledAll();
		
		$("#authDetailForm").validate({
		    errorElement: 'div',
		    errorClass: 'help-block',
		    focusInvalid: false,
		    rules: {
		    	authedUser: {
		          required: true
		       }
		    },
		
		    messages: {
		    	authedUser: {
		           required: "角色名称不能为空！"
		        }      
		    },
		
		    highlight: function (e) {
		        $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
		    },
		
		    success: function (e) {
		        $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
		        $(e).remove();
		    },
		
		    errorPlacement: function (error, element) {
		        error.insertAfter(element.parent());
		    }
		});	
		if ($("#authDetailForm").valid() == false) {
				return;
		}	
		
		
		
    	//必输项校验
		if(checkRequiredFields()){
			var sendFlag = $("#sendFlag").val();
	    	var urlVal = "${ctx}/auth/save.do";
	    	if(sendFlag==3){
	    	    //修改路径
	    	    urlVal = "${ctx}/auth/update.do";
	    	}
			saveAuthInfo(urlVal);
		}
	}
	//异步保存转授权信息
	function saveAuthInfo(urlVal){
		enabledAll();
		 $.ajax({
	        	async : false,
		    	url: urlVal,
		    	type:"post",
		    	data: $("#authDetailForm").serialize(),
		    	dataType:"json", 
		    	success: function(data){				    					    		
				   	if(data[0]=="ok"){
				   		alert(data[1]);
				   		$("#sendFlag").val("2");
				   		$("#seqNo").val(data[2]);
					   	initDetail();
					   	
					    $("#authDetailIframe").dialog("close");
						var authParams = $("#auth_info_form").serialize();
					   	$("#authListIframe").show();
					   	$("#authListIframe").load("${ctx}/auth/list.do",authParams);
				   	}else{
				   		alert(data[1]);
				   		isAdministrator();
				   	}
		    	}
	       	}); 
	}
	
	//删除操作
	function remove(){
		enabledAll();
		if(confirm("是否确定删除!")){
				$.ajax({
				async : false,
				url : "${ctx}/auth/delete.do",
				type : "post",
				data : $("#authDetailForm").serialize(),
				dataType : "json",
				success : function(data){
					if(data[0]=="ok"){
						alert(data[1]);
						$("#authDetailIframe").hide();
						 $("#authDetailIframe").dialog("close");
						var authParams = $("#auth_info_form").serialize();
					   	$("#authListIframe").show();
					   	$("#authListIframe").load("${ctx}/auth/list.do",authParams);
						
					}else{
						alert(data[1]);
					}
				}
				});
		}
		
	}
	
	//修改操作
	function modify(){
		$("#btnAModify").hide();
		$("#btnADelete").hide();
		$("#btnASave").show();
		$("#btnACancle").show();
		$("#sendFlag").val("3");
		enabledAll();
		isAdministrator();
	}
	
	function cancle(){
		
		var iscancle=window.confirm("确定取消修改？");
		if(iscancle==true){
			
			document.authDetailForm.reset();
			disabledAll();
			$("#btnAModify").show();
			$("#btnADelete").show();
			$("#btnASave").hide();
			$("#btnACancle").hide();
		}
		else{
			return;
		}
	}
	
	//必输项校验
	function checkRequiredFields(){
		var authOrg = $("#authOrg").val();
		var authUser = $("#authUser").val();
		var authedOrg = $("#authedOrg").val();
		var authedUser = $("#authedUser").val();
		var authEndTime = $("#authEndTimes").val();
		var authStatus = $("#authStatus").val();
		if(authOrg==null||authOrg==''){
		   alert("授权人所属机构不能为空！");
		   return false;
		}
		
		if(authUser==null ||authUser==''){
		   alert("授权人不能为空！");
		   return false;
		}
		
		if(authedOrg==null ||authedOrg==''){
		   alert("被授权人所属机构不能为空！");
		   return false;
		}
		
		if(authedUser==null ||authedUser==''){
		   alert("被授权人不能为空！");
		   return false;
		}
		
		if(authEndTime==null ||authEndTime==''){
		   alert("授权到期日期不能为空！");
		   return false;
		}
		
		if(authStatus==null ||authStatus==''){
		   alert("授权状态不能为空！");
		   return false;
		}
		
		if(authUser==authedUser){
			alert("授权人和被授权人不能是同一人!");
			return false;
		}

		var authRoles = document.getElementsByName("authedRole");
		var checkedRoles = 0;
		if(authRoles!=null&&authRoles.length>0){
			for(var i=0;i<authRoles.length;i++){
				if(authRoles[i].checked==true){
					checkedRoles = 1;
					break;
				}
			}
			if(checkedRoles==0){
				alert("请选择授权角色！");
				return false;
			}
		}
		return true;
	}
	

	//时间控件
    $( "#authStartTimes" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: false,
		dateFormat:'yy-mm-dd',
	});
 
    $( "#authEndTimes" ).datepicker({
		showOtherMonths: true,
		selectOtherMonths: false,
		dateFormat:'yy-mm-dd',
	});
 
</script>
<fieldset>
<div id="tab1" class="row">
	<div class="col-xs-12">
	 <form name="authDetailForm" id="authDetailForm">
	   <input type="hidden" id="sendFlag" name="sendFlag" value="${sendFlag}" />
	   <input type="hidden" id="updateOrg" name="updateOrg" value="${authInfo.updateOrg}" />
	   <input type="hidden" id="updateUser" name="updateUser" value="${authInfo.updateUser}" />
	   <input type="hidden" id="seqNo" name="seqNo" value="${authInfo.seqNo}" />
	   <input type="hidden" id="verifyCode" name="verifyCode" value="${authInfo.verifyCode}" />
	   <input type="hidden" id="isAdministrator" name="isAdministrator" value="${isAdministrator}" /> 
	   
		<div class="row" >
		
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authOrg">授权人所属机构：</label><font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
					  <input type="text" class="form-txt" id="authOrg" name="authOrg" value="${authInfo.authOrg} " />
		      		  </div>
		      		</div>
		       </div>
		       
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authUserName">授权人：</label><font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
					  	<input type="text" id="authUser" name="authUser"  value="${authInfo.authUser}" />
						<input type="text" id="authUserName" name="authUserName"  value="<@libMacro.userName userId='${authInfo.authUser}' />"/>
		              </div>
		            </div>
		       </div> 
		</div>
		<div class="row" >
		
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authedOrg">被授权人所属机构：</label><font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
						<input type="text" id="authedOrg" name="authedOrg"  value="${authInfo.authedOrg}"/>
		      		  </div>
		      		</div>
		       </div>
		       
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authedUserName">被授权人：</label><font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
					  	<input type="text" id="authedUser" name="authedUser"  value="${authInfo.authedUser}" />
					  </div>
		            </div>
		       </div> 
		</div>
		<div class="row" >
		
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authStartTime">授权开始时间：<font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
						<input type="text" id="authStartTimes" name="authStartTime"  value="${authInfo.authStartTime}"/>
					  </div>
		            </div>
		       </div> 
		       
		       <div class="form-group col-xs-6">
		       		<label class="col-sm-3 control-label no-padding-left" for="authEndTime">授权到期时间：<font color="red">*</font></label>
					<div class="col-sm-9">
					   <div class="clearfix">
						<input type="text" id="authEndTimes" name="authEndTime"  value="${authInfo.authEndTime}"/>
					   </div>
		            </div>
		       </div>
		</div>
		<div class="row" >
				
				<div class="form-group">
					<label class="col-sm-3 control-label no-padding-right" for="authStatus">授权状态：<font color="red">*</font></label>
					<div class="col-sm-9">
					  <div class="clearfix">
					    <select class="col-xs-10 col-sm-8" id="authStatus" name="authStatus">
					      <@libMacro.dictOptions dictType="D00042" currentValue="${authInfo.authStatus}" />
						</select>
					  </div>
					</div>
		         </div>
				
				  <div class="form-group">
					<label class="col-sm-3 control-label no-padding-right" for="authedRole">授权角色:<font color="red">*</font></label>
					<div class="col-sm-9">	
					  <div class="clearfix">
								     
					     <table id = "authRolesTable">
						     <div class="checkbox">
						     
						     <#if "${sendFlag== 2}">
		                         <#list roleList as role>	
		                              <#list choosedRoleList as choosedRole>
		                              	<label>	                              
		                                  <#if role.roleId==choosedRole.roleId>
		                                  		<input  type="checkbox" class="ace" name="authedRole" id="authedRole" value="${choosedRole.roleId}" onmouseout="sel();" checked="checked"/>	                               
		                                  <#else>	   
		                                    	<input  type="checkbox" class="ace" name="authedRole" id="authedRole" value="${choosedRole.roleId}" onmouseout="sel();"/>
		                                  </#if>   										
											<span class="lbl">${role.roleName}</span>
										</label>
									 </#list>
								 </#list>								 
							  <#else>									 								 
								 <#list roleList as role>	
		                              	<label>	                              
		                                  	<input  type="checkbox" class="ace" name="authedRole" id="authedRole" value="${role.roleId}" onmouseout="sel();"/>	                                										
											<span class="lbl">${role.roleName}</span>
										</label>
								 </#list>
								 					 
							  </#if> 
							 </div>
  						</table>  	
  					
					</div>
					</div>
		         </div>
		</div>
			
	</form>
	</div>
</div>	
</fieldset>	

<div id="flow_dealInfo" style="display:none;">
    <fieldset>
		<legend>审批流程信息</legend>	
		<div id="flowFrame"></div>
	</fieldset>	
</div>
<div id="chooseMemberInfo"></div>
